package com.sanhe.roller.pipe;

import static com.menu.app.util.FunctionCode.START1;
import static com.menu.app.util.FunctionCode.START2;

import com.menu.app.MyApp;
import com.menu.app.util.pipe.BasePortEventPipe;
import com.menu.app.util.port.SerialManage;
import com.sanhe.roller.util.Crc16Utils;

public class PortPipe extends BasePortEventPipe {

    static PortPipe instance;

    public static PortPipe getInstance() {
        if (instance == null) {
            instance = new PortPipe();
        }
        return instance;
    }

    private PortPipe() {
        super();
    }

    @Override
    public void doInit() {
        super.doInit();
        MyApp.getApp().toStartActivity();
        isStart = false;
    }

    private void sendTouchEvent(int i) {
        int tmp = Integer.parseInt("a" + i, 16);
        sendTouchEvent((byte) 0, (byte) tmp);
    }

    private long getValue(long l) {
        String s = l + "";
        if (s.length() > 5) {
            s = s.substring(s.length() - 5);
            return Long.valueOf(s);
        }
        return l;
    }

    @Override
    protected void sendTouchEvent(byte data_H, byte data_L) {
        byte[] tmp = {(byte) 0x83, (byte) 0x10, 1, 1, data_H, data_L};
        tmp = Crc16Utils.getData(tmp);
        byte[] touch_event = new byte[11];
        touch_event[0] = START1;
        touch_event[1] = START2;
        touch_event[2] = 8;
        System.arraycopy(tmp, 0, touch_event, 3, tmp.length);
        SerialManage.getInstance().send(touch_event);
    }
}

